package gz.com.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class CustomGateWayFilter implements GlobalFilter , Ordered {

    //过滤业务写在该方法中
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取到对应的参数值
        ServerHttpRequest request = exchange.getRequest();
//        String user = request.getQueryParams().get("user").get(0);
        String user = request.getQueryParams().getFirst("user");
        String password = request.getQueryParams().getFirst("password");
        if (!(user .equals("gz") && password .equals("123456"))) {
            //回应
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        //验证通过，放行
        return chain.filter(exchange);
    }


    //order表示过滤器执行顺序，越小执行及越高
    @Override
    public int getOrder() {
        return 0;
    }
}
